package com.dev.dream.libs.adapter;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.dev.dream.libs.adapter.app.AppController;
import com.dev.dream.libs.adapter.model.AppAdsItem;
import com.dev.dream.libs.mylib.R;

public class AppAdsAdapter  extends BaseAdapter {

	private Context context;
	private ArrayList<AppAdsItem> appAdsItem;
	//private final BitmapCache mMemoryCache;
	ImageLoader imageLoader = AppController.getInstance().getImageLoader();

	public AppAdsAdapter(Context context, ArrayList<AppAdsItem> appAdsItem) {
		this.context = context;
		this.appAdsItem = appAdsItem;
		//this.mMemoryCache = new BitmapCache();
	}

	@Override
	public int getCount() {
		return appAdsItem.size();
	}

	@Override
	public Object getItem(int position) {
		return appAdsItem.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	private class ViewHolder {
		NetworkImageView imgIcon;
		TextView txtTitle;
		ImageView img_rate;

	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;

		if (imageLoader == null)
			imageLoader = AppController.getInstance().getImageLoader();

		if (convertView == null) {
			LayoutInflater mInflater = (LayoutInflater) context
					.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
			convertView = mInflater.inflate(R.layout.row_app_ads_item, null);

			holder = new ViewHolder();

			holder.imgIcon = (NetworkImageView) convertView
					.findViewById(R.id.img_icon_app_ads);
			holder.txtTitle = (TextView) convertView
					.findViewById(R.id.tv_name_app_ads);
			holder.img_rate = (ImageView) convertView
					.findViewById(R.id.img_rate_app_ads);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();

		}

		if (position % 2 == 0) {
			convertView.setBackgroundResource(R.drawable.list_background);
		} else {
			convertView
					.setBackgroundResource(R.drawable.list_background_alternate);
		}

		// imgIcon.setImageResource(appAdsItem.get(position).getIcon());
		holder.txtTitle.setText(appAdsItem.get(position).getName());
		holder.txtTitle.setSelected(true);

		// System.out.println(appAdsItem.get(position).getRate() +"");

		if (appAdsItem.get(position).getRate().equals("rate_4")) {
			holder.img_rate.setVisibility(View.GONE);
		} else {
			holder.img_rate.setVisibility(View.VISIBLE);
		}

		if (holder.imgIcon != null) {

			holder.imgIcon.setImageUrl(appAdsItem.get(position).getUrlIcon(),
					imageLoader);
			
		}

		return convertView;
	}

//	private void setImageView(final ViewHolder viewHolder, final int position) {
//		int imageResId = appAdsItem.get(position).getIcon();
//
//		Bitmap bitmap = getBitmapFromMemCache(imageResId);
//		if (bitmap == null) {
//			bitmap = BitmapFactory.decodeResource(this.context.getResources(),
//					imageResId);
//			addBitmapToMemoryCache(imageResId, bitmap);
//		}
//		viewHolder.imgIcon.setImageBitmap(bitmap);
//	}
//
//	private void addBitmapToMemoryCache(final int key, final Bitmap bitmap) {
//		if (getBitmapFromMemCache(key) == null) {
//			mMemoryCache.put(key, bitmap);
//		}
//	}

//	private Bitmap getBitmapFromMemCache(final int key) {
//		return mMemoryCache.get(key);
//	}
//
//	class ImageDownloaderTask extends AsyncTask<String, Void, Bitmap> {
//		private final WeakReference imageViewReference;
//
//		public ImageDownloaderTask(ImageView imageView) {
//			imageViewReference = new WeakReference(imageView);
//		}
//
//		@Override
//		// Actual download method, run in the task thread
//		protected Bitmap doInBackground(String... params) {
//			// params comes from the execute() call: params[0] is the url.
//			return downloadBitmap(params[0]);
//		}
//
//		@Override
//		// Once the image is downloaded, associates it to the imageView
//		protected void onPostExecute(Bitmap bitmap) {
//			if (isCancelled()) {
//				bitmap = null;
//			}
//
//			if (imageViewReference != null) {
//				ImageView imageView = (ImageView) imageViewReference.get();
//				if (imageView != null) {
//
//					if (bitmap != null) {
//						imageView.setImageBitmap(bitmap);
//					} else {
//						imageView.setImageDrawable(imageView.getContext()
//								.getResources()
//								.getDrawable(R.drawable.ic_download));
//					}
//				}
//
//			}
//		}
//
//	}

//	public Bitmap downloadBitmap(String url) {
//		// final AndroidHttpClient client =
//		// AndroidHttpClient.newInstance("Android");
//		// final HttpGet getRequest = new HttpGet(url);
//		// try {
//		// HttpResponse response = client.execute(getRequest);
//		// final int statusCode = response.getStatusLine().getStatusCode();
//		// if (statusCode != HttpStatus.SC_OK) {
//		// Log.w("ImageDownloader", "Error " + statusCode
//		// + " while retrieving bitmap from " + url);
//		// return null;
//		// }
//		//
//		// final HttpEntity entity = response.getEntity();
//		// if (entity != null) {
//		// InputStream inputStream = null;
//		// try {
//		// inputStream = entity.getContent();
//		// final Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
//		// return bitmap;
//		// } finally {
//		// if (inputStream != null) {
//		// inputStream.close();
//		// }
//		// entity.consumeContent();
//		// }
//		// }
//		// } catch (Exception e) {
//		// // Could provide a more explicit error message for IOException or
//		// // IllegalStateException
//		// getRequest.abort();
//		// Log.w("ImageDownloader", "Error while retrieving bitmap from " +
//		// url);
//		// } finally {
//		// if (client != null) {
//		// client.close();
//		// }
//		// }
//		// return null;
//
//		Bitmap bitmap = null;
//		String urldisplay = url;
//
//		try {
//			InputStream in = new java.net.URL(urldisplay).openStream();
//			bitmap = BitmapFactory.decodeStream(in);
//
//		} catch (Exception e) {
//			Log.e("Error", e.getMessage());
//			e.printStackTrace();
//		}
//
//		return bitmap;
//	}

	

}
